package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.params.PageQueryParam;
import com.itheima.common.utils.QiniuUtils;
import com.itheima.common.vo.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.SetMeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetMealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;

//No provider available
@RestController
@RequestMapping("setMeal")
public class SetMealController {

    @Autowired
    private JedisPool jedisPool;

    //redis  set结构  交集 并集 差值 set 不能重复

    @Reference(version = "1.0")
    private SetMealService setMealService;

    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file){
        //上传的是 所有的图片 redis

        String fileName = UUID.randomUUID().toString();
        //aa.jpg
        String originalFilename = file.getOriginalFilename();
        String suffix = StringUtils.substringAfterLast(originalFilename, ".");
        fileName = fileName + "." + suffix;
        fileName = "setMeal/"+fileName;
        try {
            boolean upload = QiniuUtils.upload(file.getBytes(), fileName);
            if (upload){
                Map<String, String> map = new HashMap<>();
                map.put("fileName",fileName);
                map.put("imageUrl",QiniuUtils.prefixUrl + fileName);
                //redis使用set 存储所有的上传成功的图片
                Jedis jedis = null;
                try {
                    jedis = jedisPool.getResource();
                    jedis.sadd("SETMEAL_ALL",fileName);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if (jedis != null){
                        jedis.close();
                    }
                }
                return Result.success(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail();
    }

    @PostMapping("add")
    public Result add(@RequestBody SetMeal setMeal){
        //使用到的上传的图片  setMeal.img 图片的名称 也是七牛云 存储的文件名

        Result save = setMealService.save(setMeal);
        if (save.isSuccess()){
            //redis存储 使用的图片
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                jedis.sadd("SETMEAL_ADD",setMeal.getImg());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (jedis != null){
                    jedis.close();
                }
            }
        }
        return save;
    }
    @PostMapping("findPage")
    public Result findPage(@RequestBody PageQueryParam pageQueryParam){
        return setMealService.findPage(pageQueryParam);
    }
}
